#pragma once

#include "base_types.h"
#include <functional>
#include <map>
#include <vector>
#include <sys/epoll.h>

class CPollerObject {
public:
    CPollerObject();
    virtual ~CPollerObject();
    
    // Interface for poller events
    virtual int InputNotify() = 0;
    virtual int OutputNotify() = 0;
    virtual int HangupNotify() = 0;
    
    // Socket management
    int AttachPoller(int epoll_fd);
    int DetachPoller(int epoll_fd);
    int EnableInput();
    int EnableOutput();
    int DisableInput();
    int DisableOutput();
    
    // Get socket fd
    int GetSocketFd() const { return netfd; }
    
    // Set socket fd (protected access)
    void SetSocketFd(int fd) { netfd = fd; }
    
protected:
    int netfd;
    int epoll_fd;
    uint32_t events;
    
    // Event management
    int UpdateEvents();
};

class CPoller {
public:
    CPoller();
    ~CPoller();
    
    int Init();
    int AddObject(CPollerObject* obj);
    int RemoveObject(CPollerObject* obj);
    int WaitEvents(int timeout_ms = -1);
    void ProcessEvents();
    
private:
    int epoll_fd;
    std::vector<struct epoll_event> events;
    std::map<int, CPollerObject*> fd_to_object;
    bool initialized;
};

// Global poller instance
extern CPoller* g_poller;